package com.jg.dao.impl;

import com.jg.dao.BaseDao;
import com.jg.dao.UserDao;
import com.jg.entity.EmployeeProfiles;
import com.jg.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/3/24 11:42
 */
public class UserDaoImpl extends BaseDao implements UserDao {
    // 根据用户名密码查询用户
    @Override
    public User findUserByNameAndPassword(String username, String password) {
        String sql = "select * from users where username=? and password=?";
        Object[] params = {username, password};
        ResultSet rs = executeQuery(sql, params);
        User user = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setRole(rs.getString("role"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return user;
    }

    @Override
    public int editUser(User user) {
        String sql = "update users set username=?,password=?,role=?,email=?,phone=? where id=?";
        return executeUpdate(sql,
                user.getUsername(),
                user.getPassword(),
                user.getRole(),
                user.getEmail(),
                user.getPhone(),
                user.getId());
    }

    @Override
    public User findUserById(Integer id) {
        String sql = "select * from users where id=?";
        ResultSet rs = executeQuery(sql, id);
        User user = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setRole(rs.getString("role"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }

        }
        return user;
    }

    @Override
    public int deleteUser(Integer id) {
        String sql = "delete from users where id=?";
        return executeUpdate(sql, id);
    }

    @Override
    public User findAll() {
        String sql = "select * from users";
        ResultSet rs = executeQuery(sql);
        User user = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setRole(rs.getString("role"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return user;
    }

    @Override
    public User findUserByName() {
        String sql = "select username from users";
        ResultSet rs = executeQuery(sql);
        User user = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return user;
    }

    @Override
    public int addUser(EmployeeProfiles emp) {
        String sql = "insert into employee_profiles values(?,?,?,?,?,?,?,?)";
        return executeUpdate(sql,
                emp.getId(),
                emp.getUserId(),
                emp.getName(),
                emp.getPosition(),
                emp.getDepartment(),
                emp.getHireDate(),
                emp.getLeaveDate(),
                emp.getStatus());
    }
}
